package com.wiringweb.parameterprocessor.implementation;

import com.gooddies.utils.AnyToAnyConverter;
import com.wiringweb.parameterprocessor.ParameterProcessorInterface;
import com.wiringweb.parameterprocessor.annotation.QueryParameter;
import java.lang.annotation.Annotation;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Dmitry
 */
public class QueryParameterImpl extends ParameterProcessorInterface {

    private Class argumentType;
    private String paramName;

    public QueryParameterImpl() {
    }

    public QueryParameterImpl(Class argumentType, String paramName) {
        this.argumentType = argumentType;
        this.paramName = paramName;
    }

    @Override
    public Object process(HttpServletResponse response, HttpServletRequest request, String[] urlParameter, int parameterIndex) {
        String value = request.getParameter(paramName);
        return AnyToAnyConverter.convert(String.class, argumentType, value);
    }

    @Override
    public ParameterProcessorInterface construct(Class argumentType, Annotation annotation) {
        return new QueryParameterImpl(argumentType, ((QueryParameter) annotation).value());
    }
}
